{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "aAa7CFu0ut_D"
   },
   "source": [
    "# Déboguer le pipeline d'entraînement\n",
    "\n",
    "Ce chapitre portant sur le débogage, la langue nous importe peu ici. Nous nous intéressons surtout à la logique du code pour comprendre d'où provient l'erreur."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "66qjHa3Hut_G"
   },
   "source": [
    "Installez les bibliothèques 🤗 Transformers et 🤗 Datasets pour exécuter ce *notebook*."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "z9pfqA-kut_I"
   },
   "outputs": [],
   "source": [
    "!pip install datasets transformers[sentencepiece]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "fLcbZLV8ut_J",
    "outputId": "7d6926ff-91c6-40e3-924b-c14a9f1a5e49"
   },
   "outputs": [],
   "source": [
    "from datasets import load_dataset, load_metric\n",
    "from transformers import (\n",
    "    AutoTokenizer,\n",
    "    TFAutoModelForSequenceClassification,\n",
    ")\n",
    "\n",
    "raw_datasets = load_dataset(\"glue\", \"mnli\")\n",
    "\n",
    "model_checkpoint = \"distilbert-base-uncased\"\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)\n",
    "\n",
    "\n",
    "def preprocess_function(examples):\n",
    "    return tokenizer(examples[\"premise\"], examples[\"hypothesis\"], truncation=True)\n",
    "\n",
    "\n",
    "tokenized_datasets = raw_datasets.map(preprocess_function, batched=True)\n",
    "\n",
    "train_dataset = tokenized_datasets[\"train\"].to_tf_dataset(\n",
    "    columns=[\"input_ids\", \"labels\"], batch_size=16, shuffle=True\n",
    ")\n",
    "\n",
    "validation_dataset = tokenized_datasets[\"validation_matched\"].to_tf_dataset(\n",
    "    columns=[\"input_ids\", \"labels\"], batch_size=16, shuffle=True\n",
    ")\n",
    "\n",
    "model = TFAutoModelForSequenceClassification.from_pretrained(model_checkpoint)\n",
    "\n",
    "model.compile(loss=\"sparse_categorical_crossentropy\", optimizer=\"adam\")\n",
    "\n",
    "model.fit(train_dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "HX5TWpVeut_M",
    "outputId": "a3831754-f919-456d-c229-5cbe217b7cbe"
   },
   "outputs": [],
   "source": [
    "for batch in train_dataset:\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "iXW275EBut_O",
    "outputId": "e7523e04-ce01-4127-832b-d7282eb6615f"
   },
   "outputs": [],
   "source": [
    "model.compile(optimizer=\"adam\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "bNtmviGfut_P",
    "outputId": "b3a64beb-2bd3-4593-fb62-a035a5062cf8"
   },
   "outputs": [],
   "source": [
    "model(batch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "j9ocmoJOut_P",
    "outputId": "4eafd9ef-3285-4d3c-fa96-b8c24b2e9896"
   },
   "outputs": [],
   "source": [
    "model = TFAutoModelForSequenceClassification.from_pretrained(model_checkpoint)\n",
    "model(batch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "y1b677ecut_Q",
    "outputId": "5dc22488-04a2-4f89-e8e5-309361694d0a"
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "loss = model(batch).loss.numpy()\n",
    "indices = np.flatnonzero(np.isnan(loss))\n",
    "indices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "7Db3uwJ8ut_S",
    "outputId": "4c4e948e-3278-4c37-aa54-2a2ddb56342d"
   },
   "outputs": [],
   "source": [
    "input_ids = batch[\"input_ids\"].numpy()\n",
    "input_ids[indices]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "jYKiD7ewut_T",
    "outputId": "e5ddf829-d022-41cf-c9e7-83ddf0ed2005"
   },
   "outputs": [],
   "source": [
    "model.config.num_labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "RmzOOSMOut_U"
   },
   "outputs": [],
   "source": [
    "from tensorflow.keras.optimizers import Adam\n",
    "\n",
    "model = TFAutoModelForSequenceClassification.from_pretrained(model_checkpoint)\n",
    "model.compile(optimizer=Adam(5e-5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "ewvva33out_V",
    "outputId": "08f45348-e17d-4f1f-ad6a-53dca09f060b"
   },
   "outputs": [],
   "source": [
    "model.fit(train_dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "mZiAw85Uut_W"
   },
   "outputs": [],
   "source": [
    "input_ids = batch[\"input_ids\"].numpy()\n",
    "tokenizer.decode(input_ids[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "g3qWOkb1ut_W"
   },
   "outputs": [],
   "source": [
    "labels = batch[\"labels\"].numpy()\n",
    "label = labels[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "vicbZa70ut_W"
   },
   "outputs": [],
   "source": [
    "for batch in train_dataset:\n",
    "    break\n",
    "\n",
    "# Assurez-vous que vous avez exécuté model.compile() et défini votre optimiseur,\n",
    "# et vos pertes/métriques si vous les utilisez\n",
    "\n",
    "model.fit(batch, epochs=20)"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
